package com.wen.springbootorderserver.dao;


import com.wen.springbootorderserver.pojo.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional(rollbackFor = Exception.class)
public class OrderDatabase {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void saveOrder(Orders order)throws Exception{
        String sql = "insert into orders(order_id,name,content,create_time)values(?,?,?,?)";
        int i = jdbcTemplate.update(sql,order.getOrderId(),order.getName(),order.getContent(),order.getCreateTime());
        if (i != 1){
            throw new Exception("订单添加失败，订单库");
        }

        //本地冗余一份数据
        saveMassage(order);
    }


    public void saveMassage(Orders order)throws Exception{
        String sql = "insert into order_message(order_id,status,name,content,create_time)values(?,?,?,?,?)";
        //添加冗余记录
        int i = jdbcTemplate.update(sql,order.getOrderId(),"0",order.getName(),order.getContent(),order.getCreateTime());
        if(i != 1){
            throw new Exception("本地保存错误");
        }

    }


}
